﻿<%@ WebHandler Language="C#" Class="GovernmentOfficeProject.handler.Validate" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.LevelDataAccess;
using System.Web.SessionState;
using GovernmentOfficeProject.SysTools;
using System.Collections;
using GovernmentOfficeProject.common;

namespace GovernmentOfficeProject.handler
{
    /// <summary>
    /// Validate 用户登录验证
    /// </summary>
    public class Validate : System.Web.UI.Page ,IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            
            context.Response.ContentType = "text/plain";
            string action = context.Request.Params["Action"];
            string userName = "";
            string password = "";
            string validateCode = "";
            switch(action)
            {
                case "Login": 
                    userName = context.Request.Params["username"];
                    password = context.Request.Params["password"];
                    validateCode = context.Request.Params["validateCode"];
                    userLogin(userName, password, validateCode);
                    break;
                case "adminLogin":
                    userName = context.Request.Params["username"];
                    password = context.Request.Params["password"];
                    validateCode = context.Request.Params["validateCode"];
                    userAdminLogin(userName, password, validateCode);
                    break;
                case "Logout":
                    userLogout(false);
                    break;
                case "AdminLogout":
                    userLogout(true);
                    break;
            }
            
        }
        

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <param name="validateCode"></param>
        /// <returns></returns>
        public void userLogin(string userName, string userPassword, string validateCode)
        {
            HttpContext context = System.Web.HttpContext.Current;
            Hashtable ht = new Hashtable();
            //判断验证码
            if (Session["Vnumber"].ToString() != validateCode)
            {
                ht.Add("IsSuccess", false);
                ht.Add("Msg", "验证码错误.");
                context.Response.Write(JsonHelper.ToJson(ht));
                return;
            }
            
            Entity_USER_User user = new Entity_USER_User();
            user.LoginName = userName;
            user.LoginPassword = userPassword;
            DAL_USER_User dalUser = new DAL_USER_User();
            
            try
            {
                List<Entity_USER_User> userList = dalUser.getUserListByUserEntity(user);
                if (userList.Count > 0)
                {
                    if (userList[0].RecordStatus == 0)
                    {
                        ht.Add("IsSuccess", false);
                        ht.Add("Msg", "帐号被禁止使用,请联系管理员.");
                    }
                    else
                    {
                        SystemContext.CurrentUserID = userList[0].UserID;
                        SystemContext.CurrentUserTitle = userList[0].LoginName;
                        SessionHelper.Add(userList[0].UserID.ToString(), userList[0]);
                        ht.Add("IsSuccess", true);
                        ht.Add("Msg", "");
                    }
                    
                }
                else
                {
                    ht.Add("IsSuccess", false);
                    ht.Add("Msg", "用户名或密码错误.");
                }
            }
            catch (Exception e)
            {
                ht.Add("IsSuccess", false);
                ht.Add("Msg", "系统发生错误,请联系管理员");
                AppLog.Write(e.Message,AppLog.LogMessageType.Error);
            }
            
            context.Response.Write(JsonHelper.ToJson(ht));
        }


        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="userPassword"></param>
        /// <param name="validateCode"></param>
        /// <returns></returns>
        public void userAdminLogin(string userName, string userPassword, string validateCode)
        {
            HttpContext context = System.Web.HttpContext.Current;
            Entity_USER_User user = new Entity_USER_User();
            user.LoginName = userName;
            user.LoginPassword = userPassword;
            user.RecordStatus = 1;
            DAL_USER_User dalUser = new DAL_USER_User();
            Hashtable ht = new Hashtable();
            try
            {
                List<Entity_USER_User> userList = dalUser.getUserListByUserEntity(user);
                if (userList.Count > 0)
                {
                    if (userList[0].Backup5 != "1")
                    {
                        ht.Add("IsSuccess", false);
                        ht.Add("Msg", "对不起，您没有管理员权限.");
                    }
                    else
                    {
                        SystemContext.CurrentUserID = userList[0].UserID;
                        SystemContext.CurrentUserTitle = userList[0].LoginName;
                        SystemContext.IsAdmin = 1;
                        SessionHelper.Add(userList[0].UserID.ToString(), userList[0]);
                        ht.Add("IsSuccess", true);
                        ht.Add("Msg", "");
                    }

                }
                else
                {
                    ht.Add("IsSuccess", false);
                    ht.Add("Msg", "用户名或密码错误.");
                }
            }
            catch (Exception e)
            {
                ht.Add("IsSuccess", false);
                ht.Add("Msg", "系统发生错误,请联系管理员");
                AppLog.Write(e.Message, AppLog.LogMessageType.Error);
            }

            context.Response.Write(JsonHelper.ToJson(ht));
        }
       
        
            
       /// <summary>
       /// 退出
       /// </summary>
        public void userLogout(bool isAdmin)
        {
            HttpContext context = System.Web.HttpContext.Current;
            SessionHelper.Del(SystemContext.CurrentUserID.ToString());
            SystemContext.ClearUserStatus();
            if (isAdmin)
            {
                context.Response.Redirect("/LevelViewUI/admin/Login.htm");
            }
            else
            {
                context.Response.Redirect("/Login.htm");
            }
        }
        
        
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}